/**
 * 删除上传的图片
 */
function deleteCover(value) {
    $("#" + value).remove();
}

/**
 * 主页
 */
define(['jquery',
    'base',
    'baseUtils',
    'css!global/plugins/fileInput/fileinput.min.css',
    'plugins/fileInput/fileinput.min',
    'plugins/address/distpicker',
    'plugins/address/distpicker.data'
], function ($, base, baseUtils, _, _, _) {
    //默认加载基础库
    base();
    //执行
    return {
        info: function () {
            //身份证正面
            oneUploader("#cardFrontPictureUploader", "请选择身份证正面照片" , "#cardFrontPicture-upload-error", "#cardFrontPicture-upload-success", "cardFrontPicture");
            //身份证背面
            oneUploader("#cardBackPictureUploader", "请选择身份证背面照片" , "#cardBackPicture-upload-error", "#cardBackPicture-upload-success", "cardBackPicture");
            
            function oneUploader(node, placeholder, elErrorContainer, successContainer, name) {
                return $(node).fileinput({
                    //设置语言
                    language: 'zh',
                    //上传的地址
                    uploadUrl: '/common/uploader/index',
                    //上传方式
                    method: 'post',
                    //文件筐信息
                    msgPlaceholder: placeholder,
                    //附加属性(0图片，1文件)
                    uploadExtraData: {filesType: 0},
                    //上传类型
                    allowedFileExtensions: ['jpg', 'gif', 'png', 'jpeg'],
                    //是否显示图片展示框
                    showPreview: false,
                    //是否显示上传按钮
                    showUpload: true,
                    //是否显示取消按钮
                    showCancel: false,
                    //是否显示上传输入框
                    showCaption: true,
                    //是否显示删除键
                    showRemove: false,
                    //浏览图标
                    browseIcon: '<i class="iconfont icon-wenjianjia"></i>&nbsp;',
                    //浏览样式
                    browseClass: 'btn btn-success btn-outline',
                    //上传图标
                    uploadIcon: '<i class="iconfont icon-upload"></i>&nbsp;',
                    //上传样式
                    uploadClass: 'btn btn-success btn-outline',
                    //单位为kb，(30M)如果为0表示不限制文件大小
                    maxFileSize: 10720,
                    //错误显示位置
                    elErrorContainer: elErrorContainer,
                    //错误显示位置节点样式
                    msgErrorClass: 'alert alert-block alert-danger',
                    //验证数量
                    validateInitialCount: true
                }).on("fileuploaded", function (event, data) {
                	   if (data.response !== null && data.response.status) {
                           $(successContainer).html("<div  id=\"" + data.response.nodeId + "\"><div class=\"thumbnail uploadImageShow\"><input type=\"hidden\" name=\"" + name + "\" value=\"" + data.response.url + "\"><a href=\"" + data.response.domain + data.response.url + "\" target='_blank'><img src=\"" + data.response.domain + data.response.url + "\"></a><div class=\"btn btn-block btn-danger deletetCover\" onclick=\"deleteCover('" + data.response.nodeId + "')\">删除</div></div>");
                       } else {
                           $(elErrorContainer).text(data.response.message);
                       }
                });
            }
            
    
            /**
             * 提交食药经办人申请
             */

            $("#submitAgent").on('click', function () {
     			var reg_phone = /^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8]))\d{8}$/;//验证手机号
    			var reg_IDcard = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/; //验证身份证号
    			var reg_email = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/;//验证邮箱
            	 var name = $("#name").val();
            	 var cardType= $("#cardType").val();
            	 var cardNumber= $("#cardNumber").val();
            	 var phone= $("#phone").val();
            	 var email= $("#email").val();
            	 var cardFrontPicture= $("input[name='cardFrontPicture']").val();
            	 var cardBackPicture= $("input[name='cardBackPicture']").val();
             if(name==''){
            	$("#name-error").html("(姓名不能为空)");
            	return false;
            	}else{
            	$("#name-error").html("");
            	}
             if(cardType==''){
            	 $("#cardType-error").html("(证件类型不能为空)");
            		return false;
             }
             if(cardNumber==''){
            	 $("#cardNumber-error").html("(证件号码不能为空)"); 
            		return false;
             }else{
         		if (!reg_IDcard.test(cardNumber)){
         		  $("#cardNumber-error").html("请输入正确的证件号码");
					return false;
				}else{
				  $("#cardNumber-error").html("");
				}
             }
             if(phone==''){
            	 $("#phone-error").html("(手机号不能为空)"); 
            		return false;
             }else{
            		if (!reg_phone.test(phone)){
               		  $("#phone-error").html("(请输入正确的手机号码)");
      					return false;
      				}else{
      				  $("#phone-error").html("");	
      				}
             }
             if(email==''){
            	 $("#email-error").html("(电子邮箱不能为空)"); 
            		return false;
             }else{
         		if (!reg_email.test(email)){
             		  $("#email-error").html("(请输入正确的邮箱)");
    					return false;
    				}else{
    				  $("#email-error").html("");
    				}
             }
             if(cardFrontPicture==undefined){
            	 $("#cardFrontPicture-error").html("(身份证正面照不能为空)"); 
            		return false;
             }
             if(cardBackPicture==undefined){
                 $("#cardBackPicture-error").html("(身份证背面照不能为空)"); 
             	 return false;
             }
            	 $("#saveAgentForm").submit();
             
            });
          
        }

    } 
            
});
